<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .disable {
      cursor: not-allowed;
    }
    .active {
      background-color: #56a5f1;
      color: #fff;;
    }
  </style>
</head>
<body>
  <div id="app">
    <table border="1">
      <thead>
        <tr>
          <th>商品名称</th>
          <th>商品单价</th>
          <th>商品数量</th>
          <th>商品小计</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in goodsList">
          <td>{{item.name}}</td>
          <td>{{item.price}}</td>
          <!-- <td>{{item.num}}</td> -->
          <td><input type="text" v-model="item.num"></td>
          <td>￥{{item.price * item.num}}元</td>
        </tr>
        <tr>
          <td>总数量</td>
          <td>{{totalNum()}}</td>
          <td>总金额</td>
          <td>{{totalPrice()}}</td>
        </tr>
      </tbody>
    </table>
    <div>
      <button
        :class="{disable:currentPage === 1}"
        :disabled="currentPage === 1"
        @click="change('-')"
      >上一页</button>
      <button
        v-for="num in totalPage"
        :class="{active: currentPage === num}"
        @click="change(num)"
      >{{num}}</button>
      <button
        :class="{disable:currentPage === totalPage}"
        @click="change('+')"
        :disabled="currentPage === totalPage"
      >下一页</button>
    </div>
  </div>
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        goodsList: [
          {name: 'iphone', num: 5, price: 5999},
          {name: 'huawei', num: 15, price: 6999},
          {name: 'xiaomi', num: 10, price: 5899},
          {name: 'vivo', num: 12, price: 4599},
        ],
        totalPage: 5,
        currentPage: 4
      },
      methods: {
        totalPrice() {
          let num = 0;
          this.goodsList.forEach(item => {
            num += item.price * item.num;
          });
          return num
        },
        totalNum() {
          let num = 0;
          this.goodsList.forEach(item => {
            num += item.num * 1;
          });
          return num
        },
        // 分页，跳转页码
        change(num) {
          if(num == '-') {
            this.currentPage > 1 ? this.currentPage -- : ''
          }else if(num == '+') {
            this.currentPage < 5 ? this.currentPage ++ : ''
          }else {
            this.currentPage = num;
          }
        }
      },
    })
  </script>
</body>
</html>